package mvp.com.register.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import mvp.com.register.R;
import mvp.com.register.config.Constant;
import mvp.com.register.utils.SPManager;

public class GuideActivity extends Activity {

    private ViewPager vp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        Boolean isFirst = (Boolean) SPManager.getData(this, Constant.IS_FIRST, true);
        if (isFirst) {
            init();
        } else {
           toSplash();
        }
    }

    private void init() {
        vp = (ViewPager) findViewById(R.id.guidepager);
        LayoutInflater inflater = LayoutInflater.from(this);
        List<View> views = new ArrayList<View>();
        for (int i = 0; i < 3; i++) {
            View view = inflater.inflate(R.layout.guide_pager, null);
            ImageView iv_guide = (ImageView) view.findViewById(R.id.iv_guide_pager);
            TextView tv_guide = (TextView) view.findViewById(R.id.tv_guide_pager);
            if (i == 0) {
                iv_guide.setImageResource(R.mipmap.image_guide);
            } else if (i == 1) {
                iv_guide.setImageResource(R.mipmap.image_guide);
            } else if (i == 2) {
                iv_guide.setImageResource(R.mipmap.image_guide);
                tv_guide.setVisibility(View.VISIBLE);
                tv_guide.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // 点击进入主页后，将isFirst的Tag改成false
                        SPManager.saveData(GuideActivity.this, Constant.IS_FIRST, false);
                        toSplash();
                    }
                });
            }
            views.add(view);
        }
        vp.setAdapter(new MyPagerAdapter(views));
    }

    /**
     * 进入Splash页面
     */
    private void toSplash() {
        startActivity(new Intent(this, SplashActivity.class));
        this.finish();
    }

    /**
     * 自定义ViewPager适配器
     */
    private class MyPagerAdapter extends PagerAdapter {

        List<View> viewList;

        MyPagerAdapter(List<View> viewList) {
            this.viewList = viewList;
        }

        @Override
        public int getCount() {
            return null == viewList ? 0 : viewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position), 0);
            return viewList.get(position);
        }
    }
}
